package service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import model.ProjectInfo;
import model.User;
import service.ProjectInfoManager;

import com.opensymphony.xwork2.ActionContext;

import dao.ProjectInfoDao;
import dao.impl.*;
public class ProjectInfoManagerImpl implements ProjectInfoManager {
	private ProjectInfoDao projectInfoDao = new ProjectInfoDaoHibernate();
	public ProjectInfoDao getProjectInfoDao() {
		return projectInfoDao;
	}
	public void setProjectInfoDao(ProjectInfoDao projectInfoDao) {
		this.projectInfoDao = projectInfoDao;
	}
	public int addProjectInfoManager(String proInName, String proInProName,
			int proInledge, int proInMain, double proInRegMoney,
			double proInNeedMoney, double proInTeamMoney, Date proInTime,
			int proInState) throws Exception {
		// TODO Auto-generated method stub
			try {
				Map session = ActionContext.getContext().getSession();
				User user = (User)session.get("USER");
				ProjectInfo p = new ProjectInfo();
				p.setProInName(proInName);
				p.setProInProName(proInProName);
				p.setProInledge(proInledge);
				p.setProInMain(proInMain);
				p.setProInRegMoney(proInRegMoney);
				p.setProInNeedMoney(proInNeedMoney);
				p.setProInTeamMoney(proInTeamMoney);
				p.setProInTime(proInTime);
				p.setProInState(proInState);
				p.setUser(user);
				projectInfoDao.save(p);
				return p.getId();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				throw new Exception("新增项目信息时出现异常");
			}
	}
	public int addOrUpdateProjectInfoManager(String proInName, String proInProName,
			int proInledge, int proInMain, double proInRegMoney,
			double proInNeedMoney, double proInTeamMoney, Date proInTime,
			int proInState) throws Exception {
		// TODO Auto-generated method stub
			try {
				Map session = ActionContext.getContext().getSession();
				User user = (User)session.get("USER");
				ProjectInfo p = (ProjectInfo)session.get("P");
				if(p == null){
					p = new ProjectInfo();
				}
				p.setProInName(proInName);
				p.setProInProName(proInProName);
				p.setProInledge(proInledge);
				p.setProInMain(proInMain);
				p.setProInRegMoney(proInRegMoney);
				p.setProInNeedMoney(proInNeedMoney);
				p.setProInTeamMoney(proInTeamMoney);
				p.setProInTime(proInTime);
				p.setProInState(proInState);
				p.setUser(user);
				projectInfoDao.saveOrUpdate(p);
				return p.getId();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				throw new Exception("新增或更新项目信息时出现异常");
			}
	}
	
	public ProjectInfo getProject(int id) throws Exception{
		try {
			return projectInfoDao.get(id);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new Exception("查找项目信息时出现异常");
		}
		
	}
	public List<ProjectInfo> getProjectByUser(User user) throws Exception{
		try {
			List<ProjectInfo> pInfo = projectInfoDao.getProjectByUser(user);
			return pInfo;
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new Exception("按用户查找项目信息时出现异常");
		}
	}
	public List<ProjectInfo> getProjectByState(int state) throws Exception{
		try {
		
			List<ProjectInfo> pInfo = projectInfoDao.getByState(state);
			return pInfo;	
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new Exception("按项目的状态查找项目信息时出现异常");
		}
	}
	//更新计划书的状态
	public void updateState(ProjectInfo projectInfo) throws Exception{
		try {
			projectInfoDao.update(projectInfo);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("更改项目状态信息时出现异常");
			// TODO: handle exception
		}
	}
}
